class AIAssistant:
    def __init__(self, assistant_id, name, assistant_type, version, online, score):
        self.id = assistant_id
        self.name = name
        self.type = assistant_type
        self.version = version
        self.online = online
        self.score = score

    def __str__(self):
        status = "🟢在线" if self.online else "🔴离线"
        return f"ID: {self.id}, 名称: {self.name}, 类型: {self.type}, 版本: {self.version}, 状态: {status}, 能力评分: {self.score} [{self.type}]"

    def get_status_text(self):
        return "在线" if self.online else "离线"


class DataAnalysisAssistant(AIAssistant):
    def __init__(self, assistant_id, name, version, online, score, data_processing_speed):
        super().__init__(assistant_id, name, "数据分析助手", version, online, score)
        self.data_processing_speed = data_processing_speed  # 数据处理速度

    def __str__(self):
        base_info = super().__str__()
        return f"{base_info}, 数据处理速度: {self.data_processing_speed}MB/s"


class ImageGenerationAssistant(AIAssistant):
    def __init__(self, assistant_id, name, version, online, score, image_quality):
        super().__init__(assistant_id, name, "图片生成助手", version, online, score)
        self.image_quality = image_quality  # 图像质量等级

    def __str__(self):
        base_info = super().__str__()
        return f"{base_info}, 图像质量: {self.image_quality}/10"


class TextGenerationAssistant(AIAssistant):
    def __init__(self, assistant_id, name, version, online, score, writing_style):
        super().__init__(assistant_id, name, "文本生成助手", version, online, score)
        self.writing_style = writing_style  # 写作风格

    def __str__(self):
        base_info = super().__str__()
        return f"{base_info}, 写作风格: {self.writing_style}"


class TranslationAssistant(AIAssistant):
    def __init__(self, assistant_id, name, version, online, score, supported_languages):
        super().__init__(assistant_id, name, "翻译助手", version, online, score)
        self.supported_languages = supported_languages  # 支持的语言数量

    def __str__(self):
        base_info = super().__str__()
        return f"{base_info}, 支持语言: {self.supported_languages}种"


class VoiceAssistant(AIAssistant):
    def __init__(self, assistant_id, name, version, online, score, response_speed):
        super().__init__(assistant_id, name, "语音助手", version, online, score)
        self.response_speed = response_speed  # 响应速度

    def __str__(self):
        base_info = super().__str__()
        return f"{base_info}, 响应速度: {self.response_speed}ms"


class AIManager:
    def __init__(self, max_size=100):
        self.assistants = []
        self.max_size = max_size

    def add_assistant(self, assistant):
        if len(self.assistants) >= self.max_size:
            print("❌ 存储空间已满，无法添加新助手！")
            return False

        # 检查ID是否已存在
        for a in self.assistants:
            if a.id == assistant.id:
                print("❌ 该ID已存在，请使用其他ID！")
                return False

        self.assistants.append(assistant)
        print(f"✅ 助手【{assistant.name}】添加成功！")
        return True

    def get_all_assistants(self):
        return self.assistants

    def find_assistant_by_id(self, assistant_id):
        for assistant in self.assistants:
            if assistant.id == assistant_id:
                return assistant
        return None

    def update_assistant(self, assistant_id, attribute, value):
        assistant = self.find_assistant_by_id(assistant_id)
        if not assistant:
            print("❌ 未找到该ID对应的助手！")
            return False

        try:
            if attribute == "name":
                assistant.name = value
            elif attribute == "type":
                assistant.type = value
            elif attribute == "version":
                assistant.version = value
            elif attribute == "online":
                assistant.online = bool(int(value))
            elif attribute == "score":
                score_val = int(value)
                if 0 <= score_val <= 100:
                    assistant.score = score_val
                else:
                    print("❌ 能力评分必须在0-100之间！")
                    return False
            else:
                print("❌ 无效的属性！")
                return False

            print(f"✅ 助手【{assistant_id}】修改成功！")
            return True
        except (ValueError, TypeError):
            print("❌ 输入值格式错误！")
            return False

    def delete_assistant(self, assistant_id):
        assistant = self.find_assistant_by_id(assistant_id)
        if not assistant:
            print("❌ 未找到该ID对应的助手！")
            return False

        self.assistants = [a for a in self.assistants if a.id != assistant_id]
        print(f"✅ 助手【{assistant.name}】已删除。")
        return True

    def start_assistant(self, assistant_id):
        assistant = self.find_assistant_by_id(assistant_id)
        if not assistant:
            print("❌ 未找到该ID对应的助手！")
            return False

        if assistant.online:
            print(f"助手【{assistant.name}】已在运行。")
        else:
            assistant.online = True
            print(f"✅ 助手【{assistant.name}】已启动。")
        return True

    def stop_assistant(self, assistant_id):
        assistant = self.find_assistant_by_id(assistant_id)
        if not assistant:
            print("❌ 未找到该ID对应的助手！")
            return False

        if not assistant.online:
            print(f"助手【{assistant.name}】已停止。")
        else:
            assistant.online = False
            print(f"✅ 助手【{assistant.name}】已停止。")
        return True

    def assign_task(self, assistant_id, task):
        assistant = self.find_assistant_by_id(assistant_id)
        if not assistant:
            print("❌ 未找到该ID对应的助手！")
            return False

        if not assistant.online:
            print(f"❌ {assistant.name} 当前离线，无法执行任务。")
            return False

        print(f"✅ 任务已分配给 {assistant.name}：{task}")
        print(f"🤖 {assistant.name} 正在处理任务...")
        print(f"✅ 任务完成：{task}")
        return True

    def get_statistics(self):
        total = len(self.assistants)
        online_count = sum(1 for a in self.assistants if a.online)
        offline_count = total - online_count
        avg_score = sum(a.score for a in self.assistants) / total if total > 0 else 0

        return {
            "total": total,
            "online": online_count,
            "offline": offline_count,
            "avg_score": avg_score
        }


def create_sample_data(manager):
    """创建示例数据"""
    sample_assistants = [
        VoiceAssistant(101, "小智", "v1.2", True, 85, 200),
        TextGenerationAssistant(102, "文小助", "v2.0", True, 92, "专业"),
        ImageGenerationAssistant(103, "图灵", "v1.5", False, 88, 8),
        DataAnalysisAssistant(104, "数小析", "v1.0", True, 79, 500),
        TranslationAssistant(105, "翻译精灵", "v2.1", False, 95, 50)
    ]

    for assistant in sample_assistants:
        manager.add_assistant(assistant)


def print_menu():
    """打印菜单"""
    print("\n" + "=" * 10 + " 🤖AI助手管理器 v1.0 " + "=" * 10)
    print("1. 查看所有AI助手列表")
    print("2. 根据ID查询助手详情")
    print("3. 添加新的AI助手")
    print("4. 根据ID修改助手信息")
    print("5. 删除AI助手（按ID）")
    print("6. 启动AI助手服务")
    print("7. 停止AI助手服务")
    print("8. 为AI助手分配任务")
    print("9. 显示系统统计信息")
    print("0. 退出系统")
    print("=" * 42)


def create_assistant_interactive():
    """交互式创建助手"""
    try:
        assistant_id = int(input("请输入ID: "))
        name = input("请输入名称: ")

        print("请选择助手类型:")
        print("1. 数据分析助手")
        print("2. 图片生成助手")
        print("3. 文本生成助手")
        print("4. 翻译助手")
        print("5. 语音助手")
        type_choice = input("请选择(1-5): ")

        type_map = {
            "1": "数据分析助手",
            "2": "图片生成助手",
            "3": "文本生成助手",
            "4": "翻译助手",
            "5": "语音助手"
        }

        assistant_type = type_map.get(type_choice, "文本生成助手")
        version = input("请输入版本号: ")
        online = input("是否在线？(1-是, 0-否): ") == "1"
        score = int(input("请输入能力评分(0-100): "))

        # 根据类型创建具体的助手对象
        if assistant_type == "数据分析助手":
            speed = int(input("请输入数据处理速度(MB/s): "))
            return DataAnalysisAssistant(assistant_id, name, version, online, score, speed)
        elif assistant_type == "图片生成助手":
            quality = int(input("请输入图像质量等级(1-10): "))
            return ImageGenerationAssistant(assistant_id, name, version, online, score, quality)
        elif assistant_type == "文本生成助手":
            style = input("请输入写作风格: ")
            return TextGenerationAssistant(assistant_id, name, version, online, score, style)
        elif assistant_type == "翻译助手":
            languages = int(input("请输入支持的语言数量: "))
            return TranslationAssistant(assistant_id, name, version, online, score, languages)
        elif assistant_type == "语音助手":
            response_time = int(input("请输入响应速度(ms): "))
            return VoiceAssistant(assistant_id, name, version, online, score, response_time)
        else:
            return AIAssistant(assistant_id, name, assistant_type, version, online, score)

    except ValueError:
        print("❌ 输入格式错误！")
        return None


def main():
    manager = AIManager()
    create_sample_data(manager)

    print("欢迎使用 AI助手管理器 v1.0")

    while True:
        print_menu()
        choice = input("请选择操作: ")

        if choice == "1":
            # 查看所有助手列表
            assistants = manager.get_all_assistants()
            if not assistants:
                print("暂无AI助手数据。")
            else:
                print("所有AI助手列表：")
                for i, assistant in enumerate(assistants, 1):
                    print(f"{i}. {assistant}")

        elif choice == "2":
            # 根据ID查询助手
            try:
                assistant_id = int(input("请输入要查询的AI助手ID: "))
                assistant = manager.find_assistant_by_id(assistant_id)
                if assistant:
                    status = "🟢在线" if assistant.online else "🔴离线"
                    print(f"✅ 查询结果: {assistant}")
                else:
                    print("❌ 未找到该ID对应的助手！")
            except ValueError:
                print("❌ 请输入有效的数字ID！")

        elif choice == "3":
            # 添加新助手
            print("添加新AI助手")
            assistant = create_assistant_interactive()
            if assistant:
                manager.add_assistant(assistant)

        elif choice == "4":
            # 修改助手信息
            try:
                assistant_id = int(input("修改AI助手数据\n请输入ID: "))
                print("可修改属性列表为：name, type, version, online, score")
                input_str = input("请输入要修改的属性和值，用空格分割: ")
                parts = input_str.split()
                if len(parts) == 2:
                    attribute, value = parts
                    manager.update_assistant(assistant_id, attribute, value)
                else:
                    print("❌ 输入格式错误！")
            except ValueError:
                print("❌ 请输入有效的数字ID！")

        elif choice == "5":
            # 删除助手
            try:
                assistant_id = int(input("请输入要删除的AI助手ID: "))
                manager.delete_assistant(assistant_id)
            except ValueError:
                print("❌ 请输入有效的数字ID！")

        elif choice == "6":
            # 启动助手服务
            try:
                assistant_id = int(input("请输入要启动的AI助手ID: "))
                manager.start_assistant(assistant_id)
            except ValueError:
                print("❌ 请输入有效的数字ID！")

        elif choice == "7":
            # 停止助手服务
            try:
                assistant_id = int(input("请输入要停止的AI助手ID: "))
                manager.stop_assistant(assistant_id)
            except ValueError:
                print("❌ 请输入有效的数字ID！")

        elif choice == "8":
            # 分配任务
            try:
                assistant_id = int(input("请输入目标AI助手ID: "))
                task = input("请输入任务内容: ")
                manager.assign_task(assistant_id, task)
            except ValueError:
                print("❌ 请输入有效的数字ID！")

        elif choice == "9":
            # 显示统计信息
            stats = manager.get_statistics()
            print("系统统计信息：")
            print(f"总助手数: {stats['total']}")
            print(f"在线助手数: {stats['online']}")
            print(f"离线助手数: {stats['offline']}")
            print(f"平均能力评分: {stats['avg_score']:.2f}")

        elif choice == "0":
            # 退出系统
            print("感谢使用AI助手管理器，再见！")
            break

        else:
            print("❌ 无效的选择，请重新输入！")


if __name__ == "__main__":
    main()